package com.hexu.event.util;

import com.hexu.event.bean.AttractionsBean;
import com.hexu.event.entity.Attractions;
import com.hexu.event.service.AdministratorService;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.util.List;

import static com.hexu.event.util.ServiceUtil.attractionsService;
import static com.hexu.event.view.AdministratorView.selectIndex;


public class StageUtil {

    //取出景点的所有信息
    private static List<Attractions> listAttractionsAllInformation = attractionsService.getAttractionsInformation();

    public static void showStage(Stage stage,Scene scene,int height,int width,String title,Boolean change){
        //把场景放在舞台上
        stage.setScene(scene);
        //设置窗口的高度和宽度
        stage.setHeight(height);
        stage.setWidth(width);
        //设置窗口标题
        stage.setTitle(title);
        //设置能否改变窗口大小
        stage.setResizable(change);
        //展示场景
        stage.show();
    }

    //限制输入的只能是数字
    public static void limit(TextField textField){
        textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                Boolean flag = false;
                String textField2 = textField.getText();
                for(int i = 0;i<textField2.length() ;i++){
                    if(textField2.charAt(i)>'9' || textField2.charAt(i)<'0'){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    textField.setText(oldValue);
                }
            }
        });

    }

    //显示XX成功或失败的消息
    public static boolean display(Boolean in,Stage stage,String title){
        //XX成功
        if (in) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("消息");
            alert.setHeaderText(title+"成功");
            alert.showAndWait();
            //关闭界面
            stage.close();
            return true;
        }
        //XX失败
        else {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("消息");
            alert.setHeaderText(title+"失败");
            alert.showAndWait();
        }
        return false;
    }

    public static String time(int clicks,String time,String day){
        if(clicks %2 != 0)
            time = day;
        else
            time = null;
        return time;
    }

    //展示管理员的单独景点面版,--->这次把所有值传入，要修改就可以在Textfield中显示，是添加景点就为空
    public static void showAdministratorAttractionsTemplate(TableView table,ObservableList<AttractionsBean> data, String title, Attractions attractions)throws Exception{
        //存储要开放的时间
        String[] time = new String[7];
        //存储点击次数
        int[] clicks = new int[7];

        Stage primaryStage = new Stage();

        //设置标签
        Label labelAttractionsName = new Label("景点名字:");
        Label labelAttractionsOpenTime = new Label("景点开放时间:");
        Label labelAttractionsTicketFare = new Label("景点票价:");
        Label labelAttractionsTicketNumber = new Label("景点票数:");
        Label labelAttractionsIntroduce = new Label("景点介绍:");

        //创建空的TextField对象
        TextField attractionsName = new TextField(attractions.getName());
        TextField attractionsTicketFare = new TextField(String.valueOf(attractions.getTicketFare()));
        TextField attractionsTicketNumber = new TextField(String.valueOf(attractions.getTicketNumber()));

        //禁止修改景点名字！
        if(attractions.getName() != null){
            attractionsName.setEditable(false);
        }

        //多行输入框
        TextArea attractionsIntroduce = new TextArea(attractions.getIntroduce());
        // 设置多行输入框的推荐行数
        attractionsIntroduce.setPrefRowCount(3);

        //设置按钮
        Button sure = new Button("确定");

        //设置单选按钮和其被选情况
        RadioButton radioButton1 = new RadioButton("周一");
        if(attractions.getOpenTime().contains("周一")){
            radioButton1.setSelected(true);
            clicks[0]++;
            time[0] = StageUtil.time(clicks[0],time[0],"周一");
        }

        RadioButton radioButton2 = new RadioButton("周二");
        if(attractions.getOpenTime().contains("周二")){
            radioButton2.setSelected(true);
            clicks[1]++;
            time[1] = StageUtil.time(clicks[0],time[0],"周二");
        }

        RadioButton radioButton3 = new RadioButton("周三");
        if(attractions.getOpenTime().contains("周三")){
            radioButton3.setSelected(true);
            clicks[2]++;
            time[2] = StageUtil.time(clicks[2],time[2],"周三");
        }

        RadioButton radioButton4 = new RadioButton("周四");
        if(attractions.getOpenTime().contains("周四")){
            radioButton4.setSelected(true);
            clicks[3]++;
            time[3] = StageUtil.time(clicks[3],time[3],"周四");
        }

        RadioButton radioButton5 = new RadioButton("周五");
        if(attractions.getOpenTime().contains("周五")){
            radioButton5.setSelected(true);
            clicks[4]++;
            time[4] = StageUtil.time(clicks[4],time[4],"周五");
        }

        RadioButton radioButton6 = new RadioButton("周六");
        if(attractions.getOpenTime().contains("周六")){
            radioButton6.setSelected(true);
            clicks[5]++;
            time[5] = StageUtil.time(clicks[5],time[5],"周六");
        }

        RadioButton radioButton7 = new RadioButton("周日");
        if(attractions.getOpenTime().contains("周日")){
            radioButton7.setSelected(true);
            clicks[6]++;
            time[6] = StageUtil.time(clicks[6],time[6],"周日");
        }

        //实现按钮点击数最后为奇数才被记录
        radioButton1.addEventFilter(MouseDragEvent.MOUSE_CLICKED, event -> {

            clicks[0]++;
            time[0] = StageUtil.time(clicks[0],time[0],"周一");

        });

        radioButton2.addEventFilter(MouseDragEvent.MOUSE_CLICKED,event -> {

            clicks[1]++;
            time[1] = StageUtil.time(clicks[1],time[1],"周二");

        });

        radioButton3.addEventFilter(MouseDragEvent.MOUSE_CLICKED,event -> {

            clicks[2]++;
            time[2] = StageUtil.time(clicks[2],time[2],"周三");

        });

        radioButton4.addEventFilter(MouseDragEvent.MOUSE_CLICKED,event -> {

            clicks[3]++;
            time[3] = StageUtil.time(clicks[3],time[3],"周四");

        });

        radioButton5.addEventFilter(MouseDragEvent.MOUSE_CLICKED,event -> {

            clicks[4]++;
            time[4] = StageUtil.time(clicks[4],time[4],"周五");

        });

        radioButton6.addEventFilter(MouseDragEvent.MOUSE_CLICKED,event -> {

            clicks[5]++;
            time[5] = StageUtil.time(clicks[5],time[5],"周六");

        });

        radioButton7.addEventFilter(MouseDragEvent.MOUSE_CLICKED,event -> {

            clicks[6]++;
            time[6] = StageUtil.time(clicks[6],time[6],"周日");

        });

        //创建网格布局
        GridPane gridPane = new GridPane();
        //设置颜色
        gridPane.setStyle("-fx-background-color: #FFF5EE");

        //设置网格位置
        gridPane.add(labelAttractionsName,0,0);
        gridPane.add(attractionsName,1,0);
        gridPane.add(labelAttractionsTicketFare,0,1);
        gridPane.add(attractionsTicketFare,1,1);
        gridPane.add(labelAttractionsTicketNumber,0,2);
        gridPane.add(attractionsTicketNumber,1,2);
        gridPane.add(labelAttractionsIntroduce,0,3);
        gridPane.add(attractionsIntroduce,1,3);
        gridPane.add(labelAttractionsOpenTime,2,0);
        gridPane.add(radioButton1,2,1);
        gridPane.add(radioButton2,2,2);
        gridPane.add(radioButton3,2,3);
        gridPane.add(radioButton4,2,4);
        gridPane.add(radioButton5,2,5);
        gridPane.add(radioButton6,2,6);
        gridPane.add(radioButton7,2,7);
        gridPane.add(sure,1,4);

        //设置水平间距
        gridPane.setHgap(5);
        //设置垂直间距
        gridPane.setVgap(15);

        //进行中间对齐
        gridPane.setAlignment(Pos.CENTER);

        StageUtil.showStage(primaryStage,new Scene(gridPane),500,800,title,false);

        //限制输入的只能是数字
        StageUtil.limit(attractionsTicketFare);
        StageUtil.limit(attractionsTicketNumber);

        //设置单击事件 确定
        sure.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                //拼接一下时间
                String TIME = "";
                for(int i = 0 ;i<7 ;i++){
                    if(time[i] != null)
                        TIME = TIME+time[i];
                }

                //把输入的数据传给Attractions，强转String类型为Integer
                Attractions attractions2 = new Attractions(null,attractionsName.getText(),TIME.toString(),
                        Integer.valueOf(attractionsTicketFare.getText()),Integer.valueOf(attractionsTicketNumber.getText()),
                        attractionsIntroduce.getText(),attractions.getCommentNumber());
                //如果XX成功
                if(success(table,data,primaryStage,attractions2,title)){
                    //关闭界面
                    primaryStage.close();

                }
            }
        });

    }

    //判断是否XX成功
    private static boolean success(TableView table,ObservableList<AttractionsBean> data,Stage primaryStage, Attractions attractions,String title){
        Boolean in;
        if(title.equals("添加")){
            //判断添加注册的数据是否成功
            in = (new AdministratorService()).addAttractions(attractions);
            //添加给Table
            data.add(new AttractionsBean(attractions.getName(),attractions.getOpenTime(),attractions.getTicketFare(),
                    attractions.getTicketNumber(),attractions.getIntroduce(),attractions.getCommentNumber()));

        }
       else {
           //判断修改的数据是否成功
            in = (new AdministratorService()).updateAttractions(attractions);
            //修改显示的数据
            table.getItems().set(selectIndex,new AttractionsBean(attractions.getName(),attractions.getOpenTime()
                    ,attractions.getTicketFare(),attractions.getTicketNumber(),attractions.getIntroduce(),attractions.getCommentNumber()));
        }
        in = StageUtil.display(in,primaryStage,title);
        return in;
    }




}
